<?php
session_start ();
session_register ( 'gioHang' );

class CartController extends Zend_Controller_Action {
	
	public function init() {
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		// action body
	}
	
	public function getallsanphamAction() {
		$cartService = Application_Model_service_CartService::getInstance ();
		$sanPham = $cartService->getAllSanPham ();
		$this->view->sanPham = $sanPham;
	}
	
	public function addgiohangAction() {
		$idSanPham = $_POST ['idSanPham'];
		$soLuong = $_POST ['soLuong'];
		$cartService = Application_Model_service_CartService::getInstance ();
		try {
			if ($idSanPham != '') {
				$sanPham = $cartService->getSanPham ( $idSanPham );
				$tenSanPham = '';
				$donGia = 0;
				foreach ( $sanPham as $sp ) :
					$tenSanPham = $sp->tenSanPham;
					$donGia = $sp->giaBan;
				endforeach
				;
				
				if (! isset ( $_SESSION ['gioHang'] )) {
					$_SESSION ['gioHang'] = array ();
				}
				if (! isset ( $_SESSION ['gioHang'] [$idSanPham] )) {
					$sl = $soLuong;
				} else {
					$sl = $_SESSION ['gioHang'] [$idSanPham] + $soLuong;
				
				}
				$_SESSION ['gioHang'] [$idSanPham] = $sl;
			}
			$gioHang = $_SESSION ['gioHang'];
			$this->view->gioHang = $gioHang;
			$this->view->mess = 'Thêm sản phẩm ' . $tenSanPham . ' với số lượng ' . $soLuong . ' vào giỏ hàng';
			$this->_helper->layout ()->disableLayout ();
		} catch ( Zend_Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	public function xemgiohangAction() {
		$idSanPhamCapNhat = $_POST ['idSanPhamCapNhat'];
		$idSanPhamXoa = $_POST ['idSanPhamXoa'];
		if ($idSanPhamCapNhat != '') {
			$soLuong = $_POST ['soLuong'];
			$_SESSION ['gioHang'] [$idSanPhamCapNhat] = $soLuong;
			$this->_helper->layout ()->disableLayout ();
		}
		if ($idSanPhamXoa != '') {
			unset ( $_SESSION ['gioHang'] [$idSanPhamXoa] );
			$this->_helper->layout ()->disableLayout ();
		}
		if (count ( $_SESSION ['gioHang'] ) == 0) {
			$thongBao = "gioHangRong";
			$this->view->thongBao = $thongBao;
		}
		$gioHang = $_SESSION ['gioHang'];
		$this->view->gioHang = $gioHang;
	}
	
	public function dathangAction() {
		$idTaiKhoan = $_SESSION ['id'];
		$thongBao = '';
		if ($idTaiKhoan == '') { //neu chua dang nhap
		} else {
			$cartService = Application_Model_service_CartService::getInstance ();
			$cartService instanceof Application_Model_service_CartService;
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$chiTietPhieuDatHangService = Application_Model_service_ChiTietPhietDatHangService::getInstance ();
			$chiTietPhieuDatHangService instanceof Application_Model_service_ChiTietPhietDatHangService;
			$taiKhoanService = Application_Model_service_TaiKhoanService::getInstance ();
			$taiKhoanService instanceof Application_Model_service_TaiKhoanService;
			$gioHang = $_SESSION ['gioHang'];
			//luu xuong csdl
			if (count ( $gioHang ) > 0) {
				$tongTien = 0;
				$giaTri = $tongTien;
				$ngayTao = date ( "Y-m-d" );
				$trangThai = false;
				$biHuyHayChua = false;
				$maKhachHang = 0;
				$maTaiKhoan = $idTaiKhoan;
				$maPhieuDatHang = $phieuDatHangService->themPhieuDatHang ( $giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan );
				Foreach ( $gioHang as $id => $sl ) {
					$sanPham = $cartService->getSanPham ( $id );
					foreach ( $sanPham as $sp ) :
						$maSanPham = $sp ['maSanPham']; //ma id phát sinh tự động
						$maCuaSanPham = $sp ['maCuaSanPham']; //mã sản phẩm do người dùng nhập
						$tenSanPham = $sp ['tenSanPham'];
						$soLuong = $sl;
						$donGia = $sp ['giaBan'];
						$thanhTien = $soLuong * $donGia;
						$tongTien += $thanhTien;
						
						//goi service them tung dong chi tiet phieu dat hang
						$chiTietPhieuDatHangService->themChiTietPhieuDatHang ( $tenSanPham, $donGia, $soLuong, $thanhTien, $maSanPham, $maPhieuDatHang );
					endforeach
					;
				}
				$giaTri = $tongTien;
				$phieuDatHangService->suaPhieuDatHang ( $maPhieuDatHang, $giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan );
				unset ( $_SESSION ['gioHang'] );
			}
			
			$thongBao = "Đặt Hàng Thành Công";
		}
		$this->view->thongBao = $thongBao;
	}
	
	public function guiphieudathangAction() {
		$hoVaTen = $_POST ['hoVaTen'];
		$cmnd = $_POST ['cmnd'];
		$diaChi = $_POST ['diaChi'];
		$soDienThoai = $_POST ['soDienThoai'];
		$email = $_POST ['email'];
		$khachHangService = Application_Model_service_KhachHangService::getInstance ();
		$khachHangService instanceof Application_Model_service_KhachHangService;
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$chiTietPhieuDatHangService = Application_Model_service_ChiTietPhietDatHangService::getInstance ();
		$chiTietPhieuDatHangService instanceof Application_Model_service_ChiTietPhietDatHangService;
		$cartService = Application_Model_service_CartService::getInstance ();
		$cartService instanceof Application_Model_service_CartService;
		$gioHang = $_SESSION ['gioHang'];
		try {
			$result = $khachHangService->themKhachHang ( $hoVaTen, $cmnd, $diaChi, $soDienThoai, $email );
			if (count ( $gioHang ) > 0) {
				$tongTien = 0;
				$giaTri = $tongTien;
				$ngayTao = date ( "Y-m-d" );
				$trangThai = false;
				$biHuyHayChua = false;
				$maKhachHang = $result;
				$maTaiKhoan = 0;
				//goi service them phieu dat hang
				$maPhieuDatHang = $phieuDatHangService->themPhieuDatHang ( $giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan );
				Foreach ( $gioHang as $id => $sl ) {
					$sanPham = $cartService->getSanPham ( $id );
					foreach ( $sanPham as $sp ) :
						$maSanPham = $sp ['maSanPham']; //ma id phát sinh tự động
						$maCuaSanPham = $sp ['maCuaSanPham']; //mã sản phẩm do người dùng nhập
						$tenSanPham = $sp ['tenSanPham'];
						$soLuong = $sl;
						$donGia = $sp ['giaBan'];
						$thanhTien = $soLuong * $donGia;
						$tongTien += $thanhTien;
						
						//goi service them tung dong chi tiet phieu dat hang
						$chiTietPhieuDatHangService->themChiTietPhieuDatHang ( $tenSanPham, $donGia, $soLuong, $thanhTien, $maSanPham, $maPhieuDatHang );
					endforeach
					;
				}
				//cap nhat lai gia tri
				$giaTri = $tongTien;
				$phieuDatHangService->suaPhieuDatHang ( $maPhieuDatHang, $giaTri, $ngayTao, $trangThai, $biHuyHayChua, $maKhachHang, $maTaiKhoan );
				unset ( $_SESSION ['gioHang'] );
			}
		} catch ( Zend_Exception $e ) {
			echo "ko thanh cong--" + $e->getMessage ();
		}
	}
	
	public function updategiohangAction() {
		$gioHang = $_SESSION ['gioHang'];
		$this->view->gioHang = $gioHang;
		$this->_helper->layout ()->disableLayout ();
	}

}













